package com.example.service.impl;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.dto.VisitRecordWithStuDto;
import com.example.dto.VisitRecordWithStuPageDto;
import com.example.entity.Student;
import com.example.entity.VisitUser;
import com.example.mapper.StudentMapper;
import com.example.mapper.VisitUserMapper;
import com.example.service.VisitUserService;
import com.example.vo.VisitVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class VisitUserServiceImpl implements VisitUserService {
    @Autowired
    private VisitUserMapper visitUserMapper;

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult page(VisitRecordWithStuPageDto vpDto) {
        PageHelper.startPage(vpDto.getPageNum(), vpDto.getPageSize());
        Page<VisitVo> page =  visitUserMapper.getBySnoWithDno(vpDto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void add(VisitRecordWithStuDto visDto) {
        //1.该学号的信息在学生表存在则不存
        String sno = visDto.getSno();
        Student stu = studentMapper.findBySno(sno);
        if(stu==null){
//            Student s = new Student();
//            s.setSno(visDto.getSno());
//            s.setSsex(visDto.getSsex());
//            s.setSdept(visDto.getSdept());
//            s.setSname(visDto.getSname());
            studentMapper.add2(visDto);
        }
        //2.不在则在学生表添加该学生信息

        //3.访问表添加数据
//        VisitUser visitUser = new VisitUser();
//        visitUser.setDno(visDto.getDno());
//        visitUser.setSno(visDto.getSno());
//        visitUser.setInTime(visitUser.getInTime());
//        visitUser.setOutTime(visitUser.getOutTime());

        visitUserMapper.add2(visDto);
    }

    @Override
    public void deleteBySnoWithDno(String dno, String sno) {
        visitUserMapper.deleteBySnoWithDno(dno,sno);
    }

    @Transactional
    public void update(VisitRecordWithStuDto visDto) {
        //1.删掉stu表sno学号的信息，然后新增sno同学信息
       // String sno = visDto.getSno();
        //studentMapper.deleteBySno(sno);
//        Student s = new Student();
//        s.setSno(visDto.getSno());
//        s.setSsex(visDto.getSsex());
//        s.setSdept(visDto.getSdept());
//        s.setSname(visDto.getSname());
        //studentMapper.add2(visDto);
        studentMapper.update2(visDto);
        //2.更新vis表sno和dno为主键的信息
//        try {
//            VisitUser visitUser = new VisitUser();
//            visitUser.setSno(visDto.getSno());
//            visitUser.setDno(visitUser.getDno());
//            visitUser.setInTime(visDto.getInTime());
//            visitUser.setOutTime(visDto.getOutTime());
//            visitUserMapper.update(visitUser);
//        }catch (Exception e){
//
//        }
        visitUserMapper.update2(visDto);

    }
}
